﻿package  {
	import flash.display.Loader;
	import org.papervision3d.core.proto.DisplayObjectContainer3D;
	import org.papervision3d.objects.DisplayObject3D;
	import PaperBase;
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.primitives.*;
	import flash.display.MovieClip;
	import org.papervision3d.objects.parsers.DAE;
	import flash.events.*;
	import org.papervision3d.events.*;
	import flash.system.Security;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.LoaderInfo;
	
	import com.mdp.model.Application;
	import com.mdp.model.User;
	import com.mdp.model.Prism;
	import com.mdp.input.MainInput;
	import com.mdp.controllers.MovementController;
	
	public class main extends PaperBase {
		
	   //variables for this monster
	   public var daeFile:DAE;
	   public var envMap:DAE;
	   public var mySlides:DisplayObject3D;
	   public var planeLoader:URLLoader;
	   public var planeXML:XML;
	   public var pFrames:Array;
	   
	   public var mainInput:MainInput;
	   
	   //setup
	 	public function main() {
			trace('start');
		  init();
	   }
		
	   override protected function init3d():void {
		   
		   trace('init3d');
		   
		   User.id = stage.loaderInfo.parameters.uid;
		   
		  //START CAMERA POSITION
		  default_camera.z = -453.6;
		  default_camera.x = -262.5;
		  default_camera.y = 63;
		  default_camera.rotationY = 30;
		  default_camera.fov = 30;
		  //END CAMERA POSITION
		   
		   //init appliation stuff
		  MovementController.init(default_camera);
		  Application.init(); 
		  mainInput = new MainInput(this.stage);
		  
		  
		  planeLoader = new URLLoader();
		  planeLoader.addEventListener(Event.COMPLETE, Prism.parsePlaneXML );
		  
		  
		  if( stage.loaderInfo.parameters.planesURL != null) {
			planeLoader.load( new URLRequest(stage.loaderInfo.parameters.planesURL));
		  } else {
			planeLoader.load(new URLRequest("data/panels.xml"));
		  }
		  		  
		  cam2.rotationY = 30;
		  
		   //create the DAE in memory
		  daeFile = new DAE();
		  daeFile.addEventListener(FileLoadEvent.LOAD_COMPLETE, Prism.onCom);
		  
		  //throw new Error(stage.loaderInfo.parameters.prismURL);
		  if (stage.loaderInfo.parameters.prismURL != null) {
			daeFile.load(stage.loaderInfo.parameters.prismURL);
		  } else {
			daeFile.load("data/MDP_Minimal_001.dae");
		  }
				
		  daeFile.z = 0;
		  daeFile.x = 0;
		  daeFile.y = 0;
		  daeFile.rotationY = 30;
		  default_scene.addChild(daeFile);
		  
		  envMap = new DAE();
		  envMap.rotationY = 30;
		  envMap.y = -3000;
		  envMap.load(stage.loaderInfo.parameters.evironmentURL);
		  envMap.addEventListener( FileLoadEvent.LOAD_COMPLETE, onEnvLoad );
		  
		  default_scene.addChild(envMap);
	   }
	   
	   public function onEnvLoad( e:* = null ):void {
		   for each( var tmpMatName:String in envMap.materials.toString().split("\n")) {
			   if (tmpMatName != "") {
				   var tmpMat:Object = envMap.materials.getMaterialByName(tmpMatName);
				   tmpMat.smooth = true;
				  // tmpMat.precise = true;
			   }
		   }
	   }
	   
	   override protected function processFrame():void { 
		   
	   }
	}
}